The background is the same as “Shortest Path Algorithm:SPFA” which I compose.I think the story I compose is very vivid, hhhhhhha_
SPFA
SPFA is the abbreviation of Shortest Path First Algorithm.It can deal with graph with negative weight.But when there is a negative circle in graph, SPFA will be incompetent(we can excutive topology program first to avoid this condition when using SPFA).SPFA is a efficent algorithm but its performance is not steady, so we prefer to using Dijkstra for most of time.
We need:
- implement a queue and corresponding methods;
- an array dis to store the shortest weight to every node;
- an array vis to mark whether the node is in queue;
Attentions:
- create node 0 as a super source and the weight between it and the node can be source is 0;
- there can be more than one path between two nodes and each path may have deffierent weight.
- each array needs to be initialized
Core:
dis[x] = min{dis[x], dis[transfer] + m[transfer][x]}
if vis[x] == 0 push(x)
|
|